




use RFS_master_data, clear

gen correctedconsideration = price*quantity
egen tt = group(st0_date)

* in the paper, we use the 6 month of volume between client and dealer
* here, we use th elast 6 days (for demonstration), as we only have 1 month of pseudo data


 bys call coun tt: egen mm_vol = total(correctedconsideration)
 
 duplicates drop  call coun tt, force
 
 
 
 egen xx = group(call coun)
 
 * save the mapping
 preserve
	keep xx call coun
	duplicates drop xx, force
	save size_temp, replace
 restore
 
 
 xtset xx tt
 
 keep  tt xx   mm_vol

 
  reshape wide mm_vol , i(tt) j(xx)

  
  tsset tt
  
  * reshape wide and back/long
 foreach j of varlist mm_v* {
     replace `j' = 0 if `j' == .
	 cap drop T6`j'
	 gen T6`j' =  L.`j' + L2.`j' + L3.`j' + L4.`j' + L5.`j' + L6.`j'     
	 drop `j'
 }
 
 
  keep tt T6*
 
  reshape long T6mm_vol , i(tt) j(xx)
   
   
      xtset xx tt
	  
	  
	  merge m:1 xx using size_temp // merge in the call-coun pairs
	  /*
   not matched                             0
    matched                            39,270  (_merge==3)	  */
   drop _merge
   compress
   
   save RFS_relationships, replace
   
   
    **** COMPUTE RELATIONSHIP STRENGH MEASURES
    **** COMPUTE RELATIONSHIP STRENGH MEASURES
    **** COMPUTE RELATIONSHIP STRENGH MEASURES
    **** COMPUTE RELATIONSHIP STRENGH MEASURES
    **** COMPUTE RELATIONSHIP STRENGH MEASURES
   
 
     ** SECTION 1: from client's point of view: identify the strongest dealer-relationship
     ** SECTION 1: from client's point of view: identify the strongest dealer-relationship
     ** SECTION 1: from client's point of view: identify the strongest dealer-relationship
     ** SECTION 1: from client's point of view: identify the strongest dealer-relationship
     ** SECTION 1: from client's point of view: identify the strongest dealer-relationship
     ** SECTION 1: from client's point of view: identify the strongest dealer-relationship


 	use RFS_relationships, clear
   
   
	 * identify the largest
	 bys call tt: egen aux_max = max(T6mm_vol)
	 * identify the second largest
	 bys call tt: egen aux_max2 = max(T6mm_vol) if T6mm_vol != aux_max
	 bys call tt: egen aux_max22 = min(aux_max2)  
	 * total volume: 
	 bys call tt: egen aux_total = total(T6mm_vol)  

	 * ratio of relationship with total
	 gen rat1 = aux_max   / aux_total
	 gen rat2 = aux_max22 / aux_total
	 
	 bys call tt: gen auxn = _n
	  
	 
	 * keep the strongest relationship [along with percentage of total flow]
	 preserve
 		 keep if T6mm_vol == aux_max & aux_max < . & aux_max != 0

		  keep call tt coun rat1 
		  
		  save size_call_dealer1, replace
	 restore
	 
	 preserve
 		keep if T6mm_vol == aux_max22 & aux_max22 < . & aux_max22 != 0
		
		  keep call tt coun rat2 
		
		save size_call_dealer2, replace
	 restore	 
	 
	 
	** SECTION 2: from dealer's point of view: identify the best client
	** SECTION 2: from dealer's point of view: identify the best client
	** SECTION 2: from dealer's point of view: identify the best client
	** SECTION 2: from dealer's point of view: identify the best client
	** SECTION 2: from dealer's point of view: identify the best client
	** SECTION 2: from dealer's point of view: identify the best client
	** SECTION 2: from dealer's point of view: identify the best client
	** SECTION 2: from dealer's point of view: identify the best client
	** SECTION 2: from dealer's point of view: identify the best client
	
 	use RFS_relationships, clear
	
	 * identify the largest
	 bys coun tt: egen aux_max = max(T6mm_vol)
	 
	 * identify the second largest
	 bys coun tt: egen aux_max2 = max(T6mm_vol) if T6mm_vol != aux_max
	 bys coun tt: egen aux_max22 = min(aux_max2)  
	 
	 * identify the third largest
	 bys coun tt: egen aux_max3 = max(T6mm_vol) if T6mm_vol != aux_max & T6mm_vol != aux_max22
	 bys coun tt: egen aux_max33 = min(aux_max3)  
	 
	 * total volume: 
	 bys coun tt: egen aux_total = total(T6mm_vol)  

	 * ratio of relationship with total
	 gen deal1 = aux_max   / aux_total
	 gen deal2 = aux_max22 / aux_total
	 gen deal3 = aux_max33 / aux_total
	 
	 bys coun tt: gen auxn = _n
	 
	 
	 * keep the strongest relationship [along with percentage of total flow]
	 preserve
 		 keep if T6mm_vol == aux_max & aux_max < . & aux_max != 0

		  keep call tt coun deal1 
		  
		  save size_coun_client1, replace
	 restore
	 
	 preserve
 		keep if T6mm_vol == aux_max22 & aux_max22 < . & aux_max22 != 0
		
		  keep call tt coun deal2 
		
		save size_coun_client2, replace
	 restore	 

	 preserve
 		keep if T6mm_vol == aux_max33 & aux_max33 < . & aux_max33 != 0
		
		  keep call tt coun deal3 
		
		save size_coun_client3, replace
	 restore	 
	 
	 
	 
	 